﻿using RpcHelper;
using RpcModel;
using SqlExecHelper;
using Wedonek.RpcStore.Service.Model;

namespace Wedonek.RpcStore.Service.DAL
{
    internal class RpcControlDAL : SqlBasicClass
    {
        public RpcControlDAL() : base("RpcControlList")
        {

        }

        public int Add(RpcControl add)
        {
            base.Insert(add, out int id);
            return id;
        }

        internal bool CheckIsRepeat(string ip, int port)
        {
            return base.CheckIsExists(new ISqlWhere[] {
                    new SqlWhere("ServerIp", System.Data.SqlDbType.VarChar,15){Value=ip},
                    new SqlWhere("Port", System.Data.SqlDbType.Int){Value=port}
            });
        }

        public RpcControlInfo Get(int id)
        {
            return base.GetRow<int, RpcControlInfo>("Id", id);
        }

        public RpcControlInfo[] Query(IBasicPage paging, out long count)
        {
            paging.InitOrderBy("Id", true);
            return base.Query<RpcControlInfo>(paging.OrderBy, paging.Index, paging.Size, out count);
        }
        public void Set(int id, RpcControl set)
        {
            if (!base.Update(set, "Id", id))
            {
                throw new ErrorException("rpc.control.set.fail");
            }
        }
        public void Delete(int id)
        {
            if (!base.Drop("Id", id))
            {
                throw new ErrorException("rpc.control.drop.fail");
            }
        }
    }
}
